#include <algorithm>
#include <cstdint>
#include <iostream>

using ll = int64_t;

const ll maxn = 300 + 5, inf = 1e18;

ll m[maxn], s[maxn];
ll n, dp[maxn][maxn];

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin >> n;
    for (ll i = 1; i <= n; ++i) {
        std::cin >> m[i];
        s[i] = s[i - 1] + m[i];
        dp[i][i] = 0;
    }

    for (ll i = 1; i <= n; ++i) {
        for (ll j = i + 1; j <= n; ++j) {
            dp[i][j] = inf;
        }
    }

    for (ll len = 2; len <= n; ++len) {
        for (ll i = 1; i + len - 1 <= n; ++i) {
            ll j = i + len - 1;
            for (ll k = i; k < j; ++k) {
                dp[i][j] = std::min(dp[i][j], dp[i][k] + dp[k + 1][j] + s[j] - s[i - 1]);
            }
        }
    }

    std::cout << dp[1][n] << "\n";
}
